package cn.net.shuxing.yifu.common.front.controller;

import cn.net.shuxing.yifu.common.bean.WorkOrder;
import cn.net.shuxing.yifu.common.constant.OssConstant;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.dto.CommitOrderReq;
import cn.net.shuxing.yifu.common.dto.CommonOrderResp;
import cn.net.shuxing.yifu.common.service.OssCommonService;
import cn.net.shuxing.yifu.common.service.WorkOrderService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("/front/workorder")
public class WorkOrderController extends BaseController {

    @Autowired
    private WorkOrderService workOrderService;

    @Autowired
    private OssCommonService ossCommonService;

    /**
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/history")
    public BaseDto getHistoryOrder(Integer userId,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        BaseDto baseDto = checkUserId(userId);
        if (baseDto != null && baseDto.isError()) {
            return baseDto;
        }
        List<WorkOrder> workOrders = workOrderService.getsByUserId(userId, pageNum, pageSize);
        for (int i = 0; i < workOrders.size(); i++) {
            if (workOrders.get(i).getPhotosUrl() == null) {
                workOrders.get(i).setPhotosUrl(new String[3]);
            }
            if (workOrders.get(i).getPhoto1Url() != null) {
                String ph1 = workOrders.get(i).getPhoto1Url();
                String url = ossCommonService.getUrl(OssConstant.ossBucketName, ph1);
                workOrders.get(i).getPhotosUrl()[0] = url;
            }
            if (workOrders.get(i).getPhoto2Url() != null) {
                String ph2 = workOrders.get(i).getPhoto2Url();
                String url = ossCommonService.getUrl(OssConstant.ossBucketName, ph2);
                workOrders.get(i).getPhotosUrl()[1] = url;
            }
            if (workOrders.get(i).getPhoto3Url() != null) {
                String ph3 = workOrders.get(i).getPhoto3Url();
                String url = ossCommonService.getUrl(OssConstant.ossBucketName, ph3);
                workOrders.get(i).getPhotosUrl()[2] = url;
            }
        }
        return BaseDto.ok(workOrders);
    }

    /**
     * {
     * tag:工单标签，1充值；2提现；3其他,
     * coinType: 币种id,
     * title:标题,
     * content:问题描述,
     * photo1Urls: 图片,最多三张
     * pageNum: 页数,
     * pageSize: 本页显示条数
     * }
     *
     * @param commitOrderReq
     * @return //
     */
    @RequestMapping("/commit")
    public BaseDto commitOrder(CommitOrderReq commitOrderReq) {
        System.out.println("提交工单数据" + JSON.toJSONString(commitOrderReq));
        workOrderService.commitOrder(commitOrderReq);
        return BaseDto.ok("success");
    }

    /**
     * 获取常见问题
     *
     * @param pageNum 页数
     * @return
     */
    @RequestMapping("/common")
    public BaseDto getCommon(Integer userId,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<CommonOrderResp> commonOrderResp = workOrderService.getCommonOrderResp();

        PageInfo<CommonOrderResp> commonOrderRespPageInfo = new PageInfo<>(commonOrderResp);
        return BaseDto.ok(commonOrderRespPageInfo.getList());
    }

    @RequestMapping("/photo/upload")
    public BaseDto uploadPhoto(@RequestParam("file") MultipartFile file) throws IOException {
        String objectName = "workOrder-" + UUID.randomUUID().toString().substring(0, 10);
        File file1 = new File(new File("").getAbsolutePath() + File.separator + objectName);
        if (file1.exists())
            file1.delete();
        file1.createNewFile();
        file.transferTo(file1);
        String bucketName = OssConstant.ossBucketName;
        ossCommonService.uploadFile(bucketName, objectName, file1);
        if (file1.exists())
            file1.delete();
        return BaseDto.ok(objectName);
    }
}
